<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>cursor.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="closure_goog_db_cursor.js.html">cursor.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2012 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>/**
<a name="line16"></a> * @fileoverview Wrapper for a IndexedDB cursor.
<a name="line17"></a> *
<a name="line18"></a> */
<a name="line19"></a>
<a name="line20"></a>
<a name="line21"></a>goog.provide(&#39;goog.db.Cursor&#39;);
<a name="line22"></a>
<a name="line23"></a>goog.require(&#39;goog.async.Deferred&#39;);
<a name="line24"></a>goog.require(&#39;goog.db.Error&#39;);
<a name="line25"></a>goog.require(&#39;goog.debug&#39;);
<a name="line26"></a>goog.require(&#39;goog.events.EventTarget&#39;);
<a name="line27"></a>
<a name="line28"></a>
<a name="line29"></a>
<a name="line30"></a>/**
<a name="line31"></a> * Creates a new IDBCursor wrapper object. Should not be created directly,
<a name="line32"></a> * access cursor through object store.
<a name="line33"></a> * @see goog.db.ObjectStore#openCursor
<a name="line34"></a> *
<a name="line35"></a> * @constructor
<a name="line36"></a> * @extends {goog.events.EventTarget}
<a name="line37"></a> */
<a name="line38"></a>goog.db.Cursor = function() {
<a name="line39"></a>  goog.base(this);
<a name="line40"></a>};
<a name="line41"></a>goog.inherits(goog.db.Cursor, goog.events.EventTarget);
<a name="line42"></a>
<a name="line43"></a>
<a name="line44"></a>/**
<a name="line45"></a> * Underlying IndexedDB cursor object.
<a name="line46"></a> *
<a name="line47"></a> * @type {IDBCursor}
<a name="line48"></a> * @private
<a name="line49"></a> */
<a name="line50"></a>goog.db.Cursor.prototype.cursor_ = null;
<a name="line51"></a>
<a name="line52"></a>
<a name="line53"></a>/**
<a name="line54"></a> * Advances the cursor to the next position along its direction. When new data
<a name="line55"></a> * is available, the NEW_DATA event will be fired. If the cursor has reached the
<a name="line56"></a> * end of the range it will fire the COMPLETE event. If opt_key is specified it
<a name="line57"></a> * will advance to the key it matches in its direction.
<a name="line58"></a> *
<a name="line59"></a> * This wraps the native #continue method on the underlying object.
<a name="line60"></a> *
<a name="line61"></a> * @param {IDBKeyType=} opt_key The optional key to advance to.
<a name="line62"></a> */
<a name="line63"></a>goog.db.Cursor.prototype.next = function(opt_key) {
<a name="line64"></a>  if (opt_key) {
<a name="line65"></a>    this.cursor_[&#39;continue&#39;](opt_key);
<a name="line66"></a>  } else {
<a name="line67"></a>    this.cursor_[&#39;continue&#39;]();
<a name="line68"></a>  }
<a name="line69"></a>};
<a name="line70"></a>
<a name="line71"></a>
<a name="line72"></a>/**
<a name="line73"></a> * Updates the value at the current position of the cursor in the object store.
<a name="line74"></a> * If the cursor points to a value that has just been deleted, a new value is
<a name="line75"></a> * created.
<a name="line76"></a> *
<a name="line77"></a> * @param {*} value The value to be stored.
<a name="line78"></a> * @return {!goog.async.Deferred} The resulting deferred request.
<a name="line79"></a> */
<a name="line80"></a>goog.db.Cursor.prototype.update = function(value) {
<a name="line81"></a>  var msg = &#39;updating via cursor with value &#39;;
<a name="line82"></a>  var d = new goog.async.Deferred();
<a name="line83"></a>  var request;
<a name="line84"></a>
<a name="line85"></a>  try {
<a name="line86"></a>    request = this.cursor_.update(value);
<a name="line87"></a>  } catch (err) {
<a name="line88"></a>    msg += goog.debug.deepExpose(value);
<a name="line89"></a>    d.errback(goog.db.Error.fromException(err, msg));
<a name="line90"></a>    return d;
<a name="line91"></a>  }
<a name="line92"></a>  request.onsuccess = function(ev) {
<a name="line93"></a>    d.callback();
<a name="line94"></a>  };
<a name="line95"></a>  request.onerror = function(ev) {
<a name="line96"></a>    msg += goog.debug.deepExpose(value);
<a name="line97"></a>    d.errback(goog.db.Error.fromRequest(ev.target, msg));
<a name="line98"></a>  };
<a name="line99"></a>  return d;
<a name="line100"></a>};
<a name="line101"></a>
<a name="line102"></a>
<a name="line103"></a>/**
<a name="line104"></a> * Deletes the value at the cursor&#39;s position, without changing the cursor&#39;s
<a name="line105"></a> * position. Once the value is deleted, the cursor&#39;s value is set to null.
<a name="line106"></a> *
<a name="line107"></a> * @return {!goog.async.Deferred} The resulting deferred request.
<a name="line108"></a> */
<a name="line109"></a>goog.db.Cursor.prototype.remove = function() {
<a name="line110"></a>  var msg = &#39;deleting via cursor&#39;;
<a name="line111"></a>  var d = new goog.async.Deferred();
<a name="line112"></a>  var request;
<a name="line113"></a>
<a name="line114"></a>  try {
<a name="line115"></a>    request = this.cursor_[&#39;delete&#39;]();
<a name="line116"></a>  } catch (err) {
<a name="line117"></a>    d.errback(goog.db.Error.fromException(err, msg));
<a name="line118"></a>    return d;
<a name="line119"></a>  }
<a name="line120"></a>  request.onsuccess = function(ev) {
<a name="line121"></a>    d.callback();
<a name="line122"></a>  };
<a name="line123"></a>  request.onerror = function(ev) {
<a name="line124"></a>    d.errback(goog.db.Error.fromRequest(ev.target, msg));
<a name="line125"></a>  };
<a name="line126"></a>  return d;
<a name="line127"></a>};
<a name="line128"></a>
<a name="line129"></a>
<a name="line130"></a>/**
<a name="line131"></a> * @return {*} The value for the value at the cursor&#39;s position. Undefined
<a name="line132"></a> *     if no current value, or null if value has just been deleted.
<a name="line133"></a> */
<a name="line134"></a>goog.db.Cursor.prototype.getValue = function() {
<a name="line135"></a>  return this.cursor_[&#39;value&#39;];
<a name="line136"></a>};
<a name="line137"></a>
<a name="line138"></a>
<a name="line139"></a>/**
<a name="line140"></a> * @return {IDBKeyType} The key for the value at the cursor&#39;s position. If
<a name="line141"></a> *     the cursor is outside its range, this is undefined.
<a name="line142"></a> */
<a name="line143"></a>goog.db.Cursor.prototype.getKey = function() {
<a name="line144"></a>  return this.cursor_.key;
<a name="line145"></a>};
<a name="line146"></a>
<a name="line147"></a>
<a name="line148"></a>/**
<a name="line149"></a> * Possible cursor directions.
<a name="line150"></a> * @see http://www.w3.org/TR/IndexedDB/#idl-def-IDBCursor
<a name="line151"></a> *
<a name="line152"></a> * @enum {string}
<a name="line153"></a> */
<a name="line154"></a>goog.db.Cursor.Direction = {
<a name="line155"></a>  NEXT: &#39;next&#39;,
<a name="line156"></a>  NEXT_NO_DUPLICATE: &#39;nextunique&#39;,
<a name="line157"></a>  PREV: &#39;prev&#39;,
<a name="line158"></a>  PREV_NO_DUPLICATE: &#39;prevunique&#39;
<a name="line159"></a>};
<a name="line160"></a>
<a name="line161"></a>
<a name="line162"></a>/**
<a name="line163"></a> * Event types that the cursor can dispatch. COMPLETE events are dispatched when
<a name="line164"></a> * a cursor is depleted of values, a NEW_DATA event if there is new data
<a name="line165"></a> * available, and ERROR if an error occurred.
<a name="line166"></a> *
<a name="line167"></a> * @enum {string}
<a name="line168"></a> */
<a name="line169"></a>goog.db.Cursor.EventType = {
<a name="line170"></a>  COMPLETE: &#39;c&#39;,
<a name="line171"></a>  ERROR: &#39;e&#39;,
<a name="line172"></a>  NEW_DATA: &#39;n&#39;
<a name="line173"></a>};
</pre>


</body>
</html>
